package com.gali.juc;

import com.oracle.net.Sdp;

import java.util.concurrent.*;

/**
 * @Auther: gali
 * @Date: 2022-08-18 17:08
 * @Description:
 */
public class FutureThreadPool {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
//        extracted();
//        extracted1();
        //future调用get（）容易导致程序堵塞
        FutureTask<String> futureTask=new FutureTask<String>(()->{
            System.out.println("come in");
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "future over";
        });
        Thread t1 = new Thread(futureTask,"t1");
        t1.start();
        System.out.println(Thread.currentThread().getName()+"\t ----忙其他任务了");
        while (true){
            if (futureTask.isDone()){
                System.out.println(futureTask.get());
                break;
            }else {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("正在处理中...............");
            }
        }
    }

    private static void extracted1() throws InterruptedException, ExecutionException {
        //创建一个固定的线程池
        ExecutorService exec = Executors.newFixedThreadPool(3);
        long startTime = System.currentTimeMillis();
        FutureTask<String> futureTask=new FutureTask<String>(()->{
            try {
                TimeUnit.MILLISECONDS.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "task1 over";
        });
        exec.submit(futureTask);
        FutureTask<String> futureTask1=new FutureTask<String>(()->{
            try {
                TimeUnit.MILLISECONDS.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "task2 over";
        });
        exec.submit(futureTask1);
        try {
            TimeUnit.MILLISECONDS.sleep(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(futureTask.get());
        System.out.println(futureTask1.get());
        long endTime = System.currentTimeMillis();
        exec.shutdown();
        System.out.println("消耗的时间是："+(endTime-startTime)+"毫秒");
    }

    private static void extracted() {
        long startTime = System.currentTimeMillis();
        try {
            TimeUnit.MILLISECONDS.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try {
            TimeUnit.MILLISECONDS.sleep(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try {
            TimeUnit.MILLISECONDS.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("消耗的时间是："+(endTime-startTime)+"毫秒");
    }
}
